﻿/******************************************************************************
* File description: AnimationLayout1.qml
* Function: 纯化动画模块（包含摄像头、 按钮等布局）
* Current Version: 1.0.0
* Author: JNN
* Date: 2025-07-01
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/
import QtQuick 2.12
import QtQuick.Controls 2.12
import QtGraphicalEffects 1.12

import "../components"
import "../pageComponents"
import "./purifyAnimation"
import "../camera"
import "../chromatogram"
import "../promptBox"

Rectangle{
    id: root
    height: parent.height
    width: parent.width
    radius: 5*dp

    property bool editable: (gExpIsRunning===0 || gExpIsFailure>0) && gConnectStatus===true

    property int showCamera: 0        // 0放大显示动画 1放大显示摄像头
    property int showChrom: 0         // 0能谱图在左上角 1能谱图弹出来了

    // 右上角的尺码
    property real graphWidth: Math.max(Math.min(width, height*1.5)*0.17 * 1.3, 200*dp)
    property real graphHeight: Math.max(Math.min(width, height*1.5)*0.17, 140*dp)

    property real boxPadding: 20*dp

    signal fullScreen()

    color: "#E0E6E9"

    Text{
        x: boxPadding; y: boxPadding
        font.bold: true
        text: showCamera===1 ? "摄像头" : "实验流程"
        font.pixelSize: config.fontsize_1
        z: 100
    }

    PurifyCanvas{
        id: canvas_purify
        showDetails: showCamera === 0
        anchors.fill: root
    }
    CameraDisplay{
        id: camera_purify
        cameraName: "CameraPurify"
        cameraId: gStateCamera.cameraId_4
        visible: showCamera === 1
        anchors.fill: parent
        z: 2
    }
    Rectangle{      // 能谱图的背景颜色……
        color: Qt.rgba(233/255, 230/255, 224/255, 1)
        anchors.fill: showChrom === 1 ? root : area_corner
        visible: showChrom !== 1    // 改成显示在右上角或者脱出显示
        z: 1
    }
    Chromatogram{
        id: chromatogram
        anchors.fill: showChrom === 1 ? root : area_corner
        anchors.topMargin: 30*dp
        showDetails: 0
        visible: showChrom !== 1    // 改成显示在右上角或者脱出显示
        z: 1
    }
    MouseArea{
        id: area_corner
        height: graphHeight; width: graphWidth
        y: boxPadding; x: parent.width - width - boxPadding
        cursorShape: Qt.PointingHandCursor
        hoverEnabled: true
        onEntered: { toolTip_doubleClicked.visible = true }
        onExited: { toolTip_doubleClicked.visible = false }
        onDoubleClicked: {
            showChrom = 1 - showChrom
        }
        visible: showCamera===0 && showChrom===0
        CustomTooltip{
            id: toolTip_doubleClicked
            text: "双击放大显示"
        }
        z: 100

        Text{       // 为啥我要塞到这里呢？算了就塞到这儿吧
            visible: showCamera === 0 && showChrom === 0
            x: 10*dp; y: 10*dp
            text: "能谱图"
            font.pixelSize: config.fontsize
            font.bold: true
        }
    }
    Column{
        x: area_corner.x
        y: area_corner.y + area_corner.height + 8*dp
        width: area_corner.width
        spacing: 2*dp
        // 暂停
        TimerCard_pause{
            id: pauseModule
            z: 100
            width: area_corner.width
        }
        // 等待
        TimerCard_wait{
            id: waitModule
            z: 100
            width: area_corner.width
        }
        // 气泵
        TimerCard_air{
            id: timerCard_air
            width: area_corner.width
        }
        // 水泵
        TimerCard_water{
            id: timerCard_water
            width: area_corner.width
        }
        TimerCard_failure{
            id: timerCard_failure
            width: area_corner.width
            onClicked: {
                box_failureInfo2.show()
            }
        }
    }

    onVisibleChanged: {
        if(visible){
            canvas_purify.startAnimation()
        }else{
            canvas_purify.stopAnimation()
        }
    }

    // 返回（从摄像头）
    CustomButton{
        x: 100*dp; y: 20*dp
        width: 70*dp
        height: 30*dp
        visible: showCamera === 1
        title: "返回"
        button_color: "white"
        text_color: "#1677FF"
        text_color_hover: "#661677FF"
        button_icon: "qrc:/picture/back.png"
        onClicked: {
            showCamera = 0
        }
        z: 100
    }

    // 右下角的按钮区域
    Column{
        spacing: 16*dp
        x: parent.width - width - 20*dp
        y: parent.height - height - 20*dp
        z: 100
        CircleButton{
            visible: showCamera === 0
            icon: "qrc:/picture/animation/camera.png"
            icon_hover: "qrc:/picture/animation/camera_h.png"
            onClicked: {
                showCamera = 1
            }
            CustomTooltip{
                text: "摄像头"
                visible: parent.isHovered
            }
        }
        CircleButton{       // 全屏
            icon: "qrc:/picture/animation/fullScreen.png"
            icon_hover: "qrc:/picture/animation/fullScreen_h.png"
            onClicked: {
                root.fullScreen()
            }
            CustomTooltip{
                text: "全屏"
                visible: parent.isHovered
            }
        }
        CircleButton{       // 按钮
            id: button_showButtonList
            icon: "qrc:/picture/animation/showButtonList.png"
            icon_hover: "qrc:/picture/animation/showButtonList_h.png"
            property bool showButtonList: false
            onClicked: {
                showButtonList = !showButtonList
            }
            CustomTooltip{
                text: "操作按钮栏"
                visible: parent.isHovered
            }
        }
    }
    Item{    // 可收缩式按钮
        id: layout_button
        x: 40*dp;   width: parent.width - 2*x
        y: parent.height - 20*dp - height;     height: 50*dp
        z: 99
        clip: true
        Rectangle{
            id: button_area
            radius: 25*dp
            height: 50*dp
            width: row_button.width + 120*dp
            state: button_showButtonList.showButtonList ? "show" : "hide"
            states: [
                State {
                    name: "show"
                    PropertyChanges {
                        target: button_area
                        x: layout_button.width- width + 40*dp
                        opacity: 1
                    }
                },
                State {
                    name: "hide"
                    PropertyChanges {
                        target: button_area
                        x: layout_button.width
                        opacity: 0
                    }
                }
            ]
            transitions: [
                Transition {
                    from: "show"
                    to: "hide"
                    NumberAnimation{
                        properties: "x"
                        duration: 200
                        easing.type: Easing.InOutQuad
                    }NumberAnimation{
                        properties: "opacity"
                        duration: 200
                        easing.type: Easing.InOutQuad
                    }
                },
                Transition {
                    from: "hide"
                    to: "show"
                    NumberAnimation{
                        properties: "x"
                        duration: 200
                        easing.type: Easing.InOutQuad
                    }NumberAnimation{
                        properties: "opacity"
                        duration: 200
                        easing.type: Easing.InOutQuad
                    }
                }
            ]
            Row{
                id: row_button
                anchors.verticalCenter: parent.verticalCenter
                x: 25*dp
                height: 36*dp
                spacing: 8*dp
                CustomComboButton{
                    id: comboButton_syringe
                    editable: root.editable
                    title: "注射器复位"
                    comboBgColor: "#1677FF"
                    comboBgColor_hover: "#0958d9"
                    comboFontColor: "white"
                    height: 36*dp; width: 122*dp
                    model: ["注射器1复位", "注射器2复位", "注射器3复位", "注射器4复位", "注射器5复位", "注射器6复位", "全部复位"]
                    onTextClicked: {
                        if(gStateExp.consumableName===""){
                            box_error_common.box_text = "请先选择耗材"
                            box_error_common.show()
                            return
                        }
                        if(text === "全部复位"){
                            CMaintenance.reset("1", "9", gStateExp.consumableName)
                            CAnimationPurify.reset()
                        }
                        else{
                            let idx = text[3]
                            CAnimationPurify.resetSyringe(idx)
                            CMaintenance.reset("1", idx.toString(), gStateExp.consumableName)
                        }
                        box_resetting.show()        // 显示等待复位的弹窗
                    }
                }
                CustomButton{
                    title: "阀门全开"
                    autoWidth: true
                    height: 36*dp
                    enabled: root.editable
                    defaultMode1: true
//                    button_icon: "qrc:/picture/valve_1.png"
                    onClicked: {
                        if(gStateExp.consumableName===""){
                            box_error_common.box_text = "请先选择耗材"
                            box_error_common.show()
                            return
                        }
                        gStateOp.openPopup("02", [])
                        for(let i = 0; i < 16; i ++){
                            CAnimationPurify.changeValve(i, true)
                        }
                        CommunicationMethod.stepOperatingSimulation("4","2", ["开启","1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24"])

                    }
                }
                CustomButton{
                    title: "阀门全关"
                    autoWidth: true
                    height: 36*dp
                    enabled: root.editable
                    defaultMode1: true
//                    button_icon: "qrc:/picture/valve_1.png"
                    onClicked: {
                        if(gStateExp.consumableName===""){
                            box_error_common.box_text = "请先选择耗材"
                            box_error_common.show()
                            return
                        }
                        gStateOp.openPopup("02", [])
                        for(let i = 0; i < 16; i ++){
                            CAnimationPurify.changeValve(i, false)
                        }
                        CommunicationMethod.stepOperatingSimulation("4","2", ["关闭","1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24"])
                    }
                }
                CustomButton{
                    id: button_pPump
                    title: "转液"
                    autoWidth: true
                    height: 36*dp
                    enabled: root.editable
                    visible: gCollectContainer === 1
                    defaultMode1: true
                    onClicked: {
                        box_rudongbeng.minSpeed = 100
                        box_rudongbeng.maxSpeed = 350
                        box_rudongbeng.show()
                    }
                }
//                CustomButton{
//                    title: "蠕动泵-打液"
//                    visible: gCollectContainer === 1
//                    autoWidth: true
//                    height: 36*dp
//                    enabled: root.editable
//                    defaultMode1: true
//                    onClicked: {
//                        gStateOp.openPopup("05", [])
//                        CommunicationMethod.stepOperatingSimulation("4","6",["打液","1"])
//                        CAnimationPurify.changePPump(1);
//                    }
//                }
//                CustomButton{
//                    title: "蠕动泵-关"
//                    visible: gCollectContainer === 1
//                    autoWidth: true
//                    height: 36*dp
//                    enabled: root.editable
//                    defaultMode1: true
//                    onClicked: {
//                        gStateOp.openPopup("05", [])
//                        CommunicationMethod.stepOperatingSimulation("4","6",["关闭","1"])
//                        CAnimationPurify.changePPump(0);
//                    }
//                }
                CustomComboButton{
                    id: comboButton_wheel
                    x: 300*dp
                    editable: root.editable
                    title: "转盘转动"
                    visible: gCollectContainer === 0
                    comboBgColor: "#1677FF"
                    comboFontColor: "white"
                    height: 36*dp; width: 110*dp
                    comboBgColor_hover: "#0958d9"
                    model: ["复位", "位置1", "位置2", "位置3", "位置4", "位置5", "位置6", "位置7", "位置8", "位置9"]
                    onTextClicked: {
                        if(text === "复位"){
                            CMaintenance.reset("1", "7", gStateExp.consumableName)
                            CAnimationPurify.resetWheel(gStateExp.consumableName)
                        }else{
                            let index = Number(text[2])
                            CommunicationMethod.stepOperatingSimulation("4", "3", [index.toString()])
                            CAnimationPurify.turnWheel_posi(index-1, 1)
                        }
                    }
                }

            }
        }
    }

    onShowChromChanged: {
        if(showChrom === 1){
            box_chrom.show()
        }
    }
    Connections{
        target: mainwindow
        onGPageIdxChanged: {
            box_chrom.close()
            showChrom = 0
        }
    }

    BoxChromatogram{
        id: box_chrom
        onClosing: {
            showChrom = 0
        }
    }
}
